<?php

	namespace App\Http\Controllers\staffPermission;

	use App\Http\Controllers\Controller;
	use Illuminate\Http\Request;
	use App\Models\Staff\cStaffRole;
	use App\Models\Staff\cStaffPermission;
	use App\common\iPermission;
	use App\common\tiPermissionCheck;
	use App\common\cPermissionAuthCode;
	use App\Func\AuthFunc;


	class cStaffPermissionController extends Controller implements iPermission
	{
		use tiPermissionCheck;
		public function __construct(){

			$this->m_cHelper					= app()->gcHelper	;
			$this->m_cStaffRole					= new cStaffRole()	;
		}
	 
		/* 修改角色
		 *
		 */
		public function renameRoleName($role_id){

			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$role_id) {

					#
					#
					$j->appendProperty(['role_id'=>$role_id]);

					#
					#
					$ok_or_error	= 'error';
					$r				= $this->m_cStaffRole->renameRoleName($j);if($r === -3){
						
						$j->setDecodeStatus([[]],'renameRoleName.2001','修改角色'.'失败'.':'.'角色已经存');
					}else if($r === -1){

						$j->setDecodeStatus([[]],'renameRoleName.2002','修改角色'.'失败'.':'.'角色id错误');
					}else if($r === -2){

						$j->setDecodeStatus([[]],'renameRoleName.2002','修改角色'.'失败'.':'.'角色id不存在');
					}else{
						
						$ok_or_error = 'ok';
						$j->setDecodeStatus($r	,'renameRoleName.1000',"修改角色"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error = 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'createRoleName.2000','创建角色'.'失败');
				}
			)->getDecodeStatus(),$ok_or_error);	
		}
		
		/* 创建角色
		 *
		 */
		public function createRoleName(){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error) {
					
					$r = $this->m_cStaffRole->createRoleName($j);if($r===false){
						
						$ok_or_error = 'error';
						$j->setDecodeStatus([[]],'createRoleName.2001','创建角色'.'失败'.':'.'角色已经存');
					}else{
					
						$j->setDecodeStatus($r	,'createRoleName.1000',"创建角色"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error = 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'createRoleName.2000','创建角色'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}

		/* 删除角色
		 *
		 */
		public function revokeRoleName($role_id){

			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue(),
				function($j) use($role_id)  {
					
					$j->appendProperty(['role_id'=>$role_id]);

					#
					#
					$j->setDecodeStatus($this->m_cStaffRole->revokeRoleName($j),'revokeRoleName.1000',"删除角色"."成功");
				},
				function($j) {

					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'revokeRoleName.2000','删除角色'.'失败');
				}
			)->getDecodeStatus());	
		}

		/* 获取职员全部的角色和权限
		 *
		 */
		function getStaffOwnAllRoleAndPermission($staff_id)
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode	(
																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error,$staff_id) 
																							{
																								try
																								{
																									$r				= $this->m_cStaffRole->getStaffOwnAllRoleAndPermission($staff_id);
																								}
																								catch(\TypeError $ex)
																								{
																									$r				= false;
																								}
																								
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'getStaffOwnAllRoleAndPermission.2001','获取职员全部的角色和权限'.'失败'.':'.'职员id错误');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getStaffOwnAllRoleAndPermission.1000',"获取职员全部的角色和权限"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error,$staff_id) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getStaffOwnAllRoleAndPermission.2000','获取职员全部的角色和权限'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}

		/* 获取职员导航菜单栏
		 *
		 */
		function getstaffOwnAllNavigationMenu($staff_id)
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode (

																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error,$staff_id) 
																							{
																								try
																								{
																									$r				= $this->m_cStaffRole->getstaffOwnAllNavigationMenu($staff_id);
																								}
																								catch(\TypeError $ex)
																								{
																									$r				= false;
																								}
																								
																								#
																								#
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'getstaffOwnAllNavigationMenu.2001','获取职员导航菜单栏'.'失败'.':'.'职员id错误');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getstaffOwnAllNavigationMenu.1000',"获取职员导航菜单栏"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error,$staff_id) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getstaffOwnAllNavigationMenu.2000','获取职员导航菜单栏'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}
		/* 获取全部菜单
		 *
		 */
		function getallNavigationMenu()
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode	(

																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error) 
																							{
																								$r					= $this->m_cStaffRole->getallNavigationMenu($j);
																								
																								#
																								#
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'getallNavigationMenu.2001','获取全部菜单'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getallNavigationMenu.1000',"获取全部菜单"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getallNavigationMenu.2000','获取全部菜单'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}
		/* 获取全部权限项目
		 * 
		 */
		function getallPermissionItem()
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode	(
																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error) 
																							{
																								$r					= $this->m_cStaffRole->getallPermissionItem($j);
																								
																								#
																								#
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'getallPermissionItem.2001','获取全部权限项目'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getallPermissionItem.1000',"获取全部权限项目"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getallPermissionItem.2000','获取全部权限项目'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}

		/* 获取全部角色
		 *
		 */
		function getallRole()
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode (
																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error) 
																							{
																								$r	= $this->m_cStaffRole->getallRole($j);
																								
																								#
																								#
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'getallRole.2001','获取全部角色'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getallRole.1000',"获取全部角色"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getallRole.2000','获取全部角色'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}
		/* 获取角色包含的所有权限
		 *
		 */
		function getroleIncludedAllPermission($role_id){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$role_id) {
					
					try{
						
						$r	= $this->m_cStaffRole->getroleIncludedAllPermission($role_id);
					}catch(\TypeError $ex){
						
						$r	= false;
					}
					
					#
					#
					if($r===false){
						
						$ok_or_error	= 'error';
						$j->setDecodeStatus([[]],'getroleIncludedAllPermission.2001','获取角色包含的所有权限'.'失败'.':'.'id错误');
					}else{
					
						$j->setDecodeStatus($r	,'getroleIncludedAllPermission.1000',"获取角色包含的所有权限"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error		= 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getallRole.2000','获取角色包含的所有权限'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}
		/* 添加权限到角色
		 *
		 */
		function addpermissionToRole($role_id){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$role_id) {
					
					#
					#
					$j->appendProperty(['role_id'=>$role_id]);

					#
					#
					$r	= $this->m_cStaffRole->addpermissionToRole($j);
					
					#
					#
					if($r===false){
						
						$ok_or_error	= 'error';
						$j->setDecodeStatus([[]],'addpermissionToRole.2001','添加权限到角色'.'失败'.':'.'');
					}else{
					
						$j->setDecodeStatus($r	,'addpermissionToRole.1000',"添加权限到角色"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error		= 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'addpermissionToRole.2000','添加权限到角色'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}

		/* 删除角色中包含的权限
		 *
		 */
		function revokePermissionFromRole($role_id)
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode	(
																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error,$role_id) 
																							{
																								#
																								#
																								$j->appendProperty(['role_id'=>$role_id]);

																								#
																								#
																								$r					= $this->m_cStaffRole->revokePermissionFromRole($j);
																								
																								#
																								#
																								if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'revokePermissionFromRole.2001','删除角色中包含的权限'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'revokePermissionFromRole.1000',"删除角色中包含的权限"."成功");
																								}
																							}
																							,
																							function($j) use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'revokePermissionFromRole.2000','删除角色中包含的权限'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}

		/* 添加角色给职员
		 *
		 */
		function addroleToStaff($staff_id){
            AuthFunc::isSupperAdmin();
			return $this->m_cHelper->gfApiEchoEx (
				
													$this->m_cHelper->gfJsonDecode (
																						$this->m_cHelper->gfGetRequestBodyValue()
																						,
																						function($j) use(&$ok_or_error,$staff_id) 
																						{
																							#
																							#
																							$j->appendProperty(['staff_id'=>$staff_id]);

																							#
																							#
																							$r	= $this->m_cStaffRole->addroleToStaffEx($j);
																							
																							#
																							#
																							if($r===false)
																							{
																								$ok_or_error	= 'error';
																								$j->setDecodeStatus([[]],'addroleToStaff.2001','添加角色给职员'.'失败'.':'.'');
																							}
																							else
																							{
																								$j->setDecodeStatus($r	,'addroleToStaff.1000',"添加角色给职员"."成功");
																							}
																						}
																						,
																						function($j)  use(&$ok_or_error) 
																						{
																							$ok_or_error		= 'error';
																							$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'addroleToStaff.2000','添加角色给职员'.'失败');
																						}
													)->getDecodeStatus()
													,
													$ok_or_error
			);	
		}

		/* 删除职员的角色
		 *
		 */
		function revokeRoleFromStaff($staff_id){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$staff_id)
				{
					#
					#
					$j->appendProperty(['staff_id'=>$staff_id]);

					#
					#					
					$r	= $this->m_cStaffRole->revokeRoleFromStaffEx($j);
					
					#
					#
					if($r===false)
					{
						$ok_or_error	= 'error';
						$j->setDecodeStatus([[]],'revokeRoleFromStaff.2001','删除职员的角色'.'失败'.':'.'');
					}
					else
					{
						$j->setDecodeStatus($r	,'revokeRoleFromStaff.1000',"删除职员的角色"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error		= 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'revokeRoleFromStaff.2000','删除职员的角色'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}

		/* 创建导航菜单栏
		 *
		 */
		function addnavigationMenuItem()
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode		(
																								$this->m_cHelper->gfGetRequestBodyValue()
																								,
																								function($j) use(&$ok_or_error) 
																								{
																									$r	= $this->m_cStaffRole->addnavigationMenuItem($j);
																									
																									#
																									#
																									if($r===false)
																									{
																										$ok_or_error	= 'error';
																										$j->setDecodeStatus([[]],'addnavigationMenuItem.2001','创建导航菜单栏'.'失败'.':'.'');
																									}
																									else
																									{
																										$j->setDecodeStatus($r	,'addnavigationMenuItem.1000',"创建导航菜单栏"."成功");
																									}
																								}
																								,
																								function($j)  use(&$ok_or_error) 
																								{
																									$ok_or_error		= 'error';
																									$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'addnavigationMenuItem.2000','创建导航菜单栏'.'失败');
																								}
																							)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}
		/* 删除导航菜单栏
		 *
		 */
		function revokeNavigationMenuItem($menu_id){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$menu_id) {
					
					try{
						
						$r	= $this->m_cStaffRole->revokeNavigationMenuItem($menu_id);
					}catch(\TypeError $ex){
						
						$r	= false;
					}
					
					#
					#
					if($r===false){
						
						$ok_or_error	= 'error';
						$j->setDecodeStatus([[]],'revokeNavigationMenuItem.2001','删除导航菜单栏'.'失败'.':'.'');
					}else{
					
						$j->setDecodeStatus($r	,'revokeNavigationMenuItem.1000',"删除导航菜单栏"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error,$menu_id) {
					
					$ok_or_error		= 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'revokeNavigationMenuItem.2000','删除导航菜单栏'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}
	
		/* 创建权限项目
		 *
		 */
		function addonePermissionItem()
		{
			return $this->m_cHelper->gfApiEchoEx	(
														$this->m_cHelper->gfJsonDecode	(

																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error) 
																							{
																								$r	= $this->m_cStaffRole->addonePermissionItem($j);if($r===false)
																								{
																									$ok_or_error	= 'error';
																									$j->setDecodeStatus([[]],'addonePermissionItem.2001','创建权限项目'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'addonePermissionItem.1000',"创建权限项目"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';
																								$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'addonePermissionItem.2000','创建权限项目'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}


		/* 删除权限项目
		 *
		 */
		function revokePermissionItem($permission_id){
			
			return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

				$this->m_cHelper->gfGetRequestBodyValue()
				,
				function($j) use(&$ok_or_error,$permission_id) {
					
					#
					#
					$j->appendProperty(['permission_id'=>$permission_id]);
					
					#
					#
					$r	= $this->m_cStaffRole->revokePermissionItem($j);
					
					#
					#
					if($r===false){
						
						$ok_or_error	= 'error';
						$j->setDecodeStatus([[]],'revokePermissionItem.2001','删除权限项目'.'失败'.':'.'');
					}else{
					
						$j->setDecodeStatus($r	,'revokePermissionItem.1000',"删除权限项目"."成功");
					}
				}
				,
				function($j)  use(&$ok_or_error) {
					
					$ok_or_error		= 'error';
					$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'revokePermissionItem.2000','删除权限项目'.'失败');}
			)->getDecodeStatus(),$ok_or_error);	
		}

		/* 获取职员拥有的全部角色
		 *
		 */
		function getstaffOwnAllRole($staff_id){
			
			return $this->m_cHelper->gfApiEchoEx	(
				
														$this->m_cHelper->gfJsonDecode	(
																							$this->m_cHelper->gfGetRequestBodyValue()
																							,
																							function($j) use(&$ok_or_error,$staff_id) 
																							{
																								$j->appendProperty(['staff_id'=>$staff_id]);
																								$r = $this->m_cStaffRole->getstaffOwnAllRole($j);if($r===false)
																								{
																									$ok_or_error	= 'error';$j->setDecodeStatus([[]],'getstaffOwnAllRole.2001','获取职员拥有的全部角色'.'失败'.':'.'');
																								}
																								else
																								{
																									$j->setDecodeStatus($r	,'getstaffOwnAllRole.1000',"获取职员拥有的全部角色"."成功");
																								}
																							}
																							,
																							function($j)  use(&$ok_or_error) 
																							{
																								$ok_or_error		= 'error';$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getstaffOwnAllRole.2000','获取职员拥有的全部角色'.'失败');
																							}
																						)->getDecodeStatus()
														,
														$ok_or_error
													);	
		}








	}
